package com.infindo.frame.util;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.content.Context;

public class DateUtil {
	public DateUtil() {
	}

	/**
	 * 2009-05-08 10:00:00 +0200
	 * @param datewes
	 * 
	 * "raceDate     ":"2010-04-18 03:00:00 -0400",
	 * "raceLocalDate":"2010-04-18 15:00:00 "
	 * @return
	 */
	public static Date getDate(String date) {
		Date ret = null;

		Calendar calendar = Calendar.getInstance();
		if(date.length()>=25){
			calendar.setTimeZone(TimeZone.getTimeZone("GMT" + date.substring(20, 25)));
			calendar.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)));
			calendar.set(Calendar.MONTH, Integer.parseInt(date.substring(5, 7)) - 1);
			calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date.substring(8, 10)));
			calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(date.substring(11, 13)));
			calendar.set(Calendar.MINUTE, Integer.parseInt(date.substring(14, 16)));
			calendar.set(Calendar.SECOND, Integer.parseInt(date.substring(17, 19)));
		}
		else{
			calendar.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)));
			calendar.set(Calendar.MONTH, Integer.parseInt(date.substring(5, 7)) - 1);
			calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date.substring(8, 10)));
			calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(date.substring(11, 13)));
			calendar.set(Calendar.MINUTE, Integer.parseInt(date.substring(14, 16)));
			calendar.set(Calendar.SECOND, Integer.parseInt(date.substring(17, 19)));
		}
		ret = calendar.getTime();
		return ret;
	}
	
	public static int getDay(int year,int month){
		Date theDate = DateUtil.getDate(year+"-"+(month>9?(""+month):("0"+month))+"-"+"01 00:00:00 +0000");
		return DateFormat.dateToDay(theDate);
	}

	public static String getWeekStr(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int week = c.get(Calendar.DAY_OF_WEEK);
		String ret = null;
		if (week == Calendar.MONDAY) {
			ret = "Mon";
		} else if (week == Calendar.TUESDAY) {
			ret = "Tue";
		} else if (week == Calendar.WEDNESDAY) {
			ret = "Wed";
		} else if (week == Calendar.THURSDAY) {
			ret = "Thu";
		} else if (week == Calendar.FRIDAY) {
			ret = "Fri";
		} else if (week == Calendar.SATURDAY) {
			ret = "Sat";
		} else if (week == Calendar.SUNDAY) {
			ret = "Sun";
		}
		return ret;
	}
	
	public static String getWeekALL(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int week = c.get(Calendar.DAY_OF_WEEK);
		String ret = null;
		if (week == Calendar.MONDAY) {
			ret = "Monday";
		} else if (week == Calendar.TUESDAY) {
			ret = "Tuesday";
		} else if (week == Calendar.WEDNESDAY) {
			ret = "Wednesday";
		} else if (week == Calendar.THURSDAY) {
			ret = "Thursday";
		} else if (week == Calendar.FRIDAY) {
			ret = "Friday";
		} else if (week == Calendar.SATURDAY) {
			ret = "Saturday";
		} else if (week == Calendar.SUNDAY) {
			ret = "Sunday";
		}
		return ret;
	}
	
	public static String getWeekAllStr(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int week = c.get(Calendar.DAY_OF_WEEK);
		String ret = null;
		if (week == Calendar.MONDAY) {
			ret = "Monday";
		} else if (week == Calendar.TUESDAY) {
			ret = "Tuesday";
		} else if (week == Calendar.WEDNESDAY) {
			ret = "Wednesday";
		} else if (week == Calendar.THURSDAY) {
			ret = "Thursday";
		} else if (week == Calendar.FRIDAY) {
			ret = "Friday";
		} else if (week == Calendar.SATURDAY) {
			ret = "Saturday";
		} else if (week == Calendar.SUNDAY) {
			ret = "Sunday";
		}
		return ret;
	}

//	public static String getMonthStr(Date date) {
//		Calendar c = Calendar.getInstance();
//		c.setTime(date);
//		int month = c.get(Calendar.MONTH);
//		String ret = null;
//		if (month == Calendar.JANUARY) {
//			ret = "January";
//		} else if (month == Calendar.FEBRUARY) {
//			ret = "February";
//		} else if (month == Calendar.MARCH) {
//			ret = "March";
//		} else if (month == Calendar.APRIL) {
//			ret = "April";
//		} else if (month == Calendar.MAY) {
//			ret = "May";
//		} else if (month == Calendar.JUNE) {
//			ret = "June";
//		} else if (month == Calendar.JULY) {
//			ret = "July";
//		} else if (month == Calendar.AUGUST) {
//			ret = "August";
//		} else if (month == Calendar.SEPTEMBER) {
//			ret = "September";
//		} else if (month == Calendar.OCTOBER) {
//			ret = "Oct";
//		} else if (month == Calendar.NOVEMBER) {
//			ret = "November";
//		} else if (month == Calendar.DECEMBER) {
//			ret = "December";
//		}
//		return ret;
//	}
	
	public static String getMonthStr(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = c.get(Calendar.MONTH);
		String ret = null;
		if (month == Calendar.JANUARY) {
			ret = "Jan";
		} else if (month == Calendar.FEBRUARY) {
			ret = "Feb";
		} else if (month == Calendar.MARCH) {
			ret = "Mar";
		} else if (month == Calendar.APRIL) {
			ret = "Apr";
		} else if (month == Calendar.MAY) {
			ret = "May";
		} else if (month == Calendar.JUNE) {
			ret = "Jun";
		} else if (month == Calendar.JULY) {
			ret = "Jul";
		} else if (month == Calendar.AUGUST) {
			ret = "Aug";
		} else if (month == Calendar.SEPTEMBER) {
			ret = "Sep";
		} else if (month == Calendar.OCTOBER) {
			ret = "Oct";
		} else if (month == Calendar.NOVEMBER) {
			ret = "Nov";
		} else if (month == Calendar.DECEMBER) {
			ret = "Dec";
		}
		return ret;
	}
	
	public static String getMonthString(int month){
		String ret = null;
		month = month -1;
		if (month == Calendar.JANUARY) {
			ret = "January";
		} else if (month == Calendar.FEBRUARY) {
			ret = "February";
		} else if (month == Calendar.MARCH) {
			ret = "March";
		} else if (month == Calendar.APRIL) {
			ret = "April";
		} else if (month == Calendar.MAY) {
			ret = "May";
		} else if (month == Calendar.JUNE) {
			ret = "June";
		} else if (month == Calendar.JULY) {
			ret = "July";
		} else if (month == Calendar.AUGUST) {
			ret = "August";
		} else if (month == Calendar.SEPTEMBER) {
			ret = "September";
		} else if (month == Calendar.OCTOBER) {
			ret = "Oct";
		} else if (month == Calendar.NOVEMBER) {
			ret = "November";
		} else if (month == Calendar.DECEMBER) {
			ret = "December";
		}
		return ret;
	}
	
	public static String getMonthStr(int month){
		String ret = null;
		month = month -1;
		if (month == Calendar.JANUARY) {
			ret = "Jan";
		} else if (month == Calendar.FEBRUARY) {
			ret = "Feb";
		} else if (month == Calendar.MARCH) {
			ret = "Mar";
		} else if (month == Calendar.APRIL) {
			ret = "Apr";
		} else if (month == Calendar.MAY) {
			ret = "May";
		} else if (month == Calendar.JUNE) {
			ret = "Jun";
		} else if (month == Calendar.JULY) {
			ret = "Jul";
		} else if (month == Calendar.AUGUST) {
			ret = "Aug";
		} else if (month == Calendar.SEPTEMBER) {
			ret = "Sep";
		} else if (month == Calendar.OCTOBER) {
			ret = "Oct";
		} else if (month == Calendar.NOVEMBER) {
			ret = "Nov";
		} else if (month == Calendar.DECEMBER) {
			ret = "Dec";
		}
		return ret;
	}
	
	
	public static String getYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		String year;
		year = "" + c.get(Calendar.YEAR);
		return year;
	}
	
	public static String getMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = c.get(Calendar.MONTH);
		String ret = null;
		if (month == Calendar.JANUARY) {
			ret = "Jan";
		} else if (month == Calendar.FEBRUARY) {
			ret = "Feb";
		} else if (month == Calendar.MARCH) {
			ret = "Mar";
		} else if (month == Calendar.APRIL) {
			ret = "Apr";
		} else if (month == Calendar.MAY) {
			ret = "May";
		} else if (month == Calendar.JUNE) {
			ret = "Jun";
		} else if (month == Calendar.JULY) {
			ret = "Jul";
		} else if (month == Calendar.AUGUST) {
			ret = "Aug";
		} else if (month == Calendar.SEPTEMBER) {
			ret = "Sep";
		} else if (month == Calendar.OCTOBER) {
			ret = "Oct";
		} else if (month == Calendar.NOVEMBER) {
			ret = "Nov";
		} else if (month == Calendar.DECEMBER) {
			ret = "Dec";
		}
		return ret;
	}
	
	public static String getMonthDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int day = c.get(Calendar.DAY_OF_MONTH);
		String monthDay = String.valueOf(day);
		return monthDay;
	}
	
	public static String getTime(Date date) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date);
		String min = "" + cal1.get(Calendar.MINUTE);
		String hour = "" + cal1.get(Calendar.HOUR_OF_DAY);
		String timestr = "";
		if (min.length() < 2) {
			min = min+"0";
		} else if(hour.length()<2){
			hour = "0"+hour;
		}
		timestr = hour+":"+min;
		return timestr;
	}
	public static String getUIAMPMTime(Date date) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date);
		int min =  cal1.get(Calendar.MINUTE);
		int hour =  cal1.get(Calendar.HOUR_OF_DAY);
		String minStr = "";
		String hourStr = "";
		String timeStr = "";
		if(hour<12){
			if(min<10){
				minStr = "0"+min+" am ";
			}else{
				minStr = ""+min+" am ";
			}
			if(hour<10){
				hourStr = "0"+hour;
			}else{
				hourStr = ""+hour;
			}
			timeStr = hourStr+minStr;
		}else{
			hour = hour%12;
			if(min<10){
				minStr = "0"+min+" pm ";
			}else{
				minStr = ""+min+" pm ";
			}
			if(hour<10){
				hourStr = "0"+hour;
			}else{
				hourStr = ""+hour;
			}
		}
		timeStr= hourStr+":"+minStr;
		return timeStr;
	}
	
	
	public static String getTime(String date) {
		Date qualitydate = DateUtil.getDate(date);
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(qualitydate);
		String min = "" + cal1.get(Calendar.MINUTE);
		String timestr = "";
		if (min.length() < 2) {
			timestr = "" + cal1.get(Calendar.HOUR_OF_DAY) + ":"
					+ cal1.get(Calendar.MINUTE) + "0";
		} else {
			timestr = "" + cal1.get(Calendar.HOUR_OF_DAY) + ":"
					+ cal1.get(Calendar.MINUTE);
		}
		return timestr;
	}
	
	public static String getShowTime(int time){
		if (time>9) {
			return ""+time;
		} else {
			return "0"+time;
		}
	}
	
	public static String getUIDateTime(Date date){
		String month = getMonthStr(date);
		String time = getMonthDay(date);
		String year = getYear(date);
		
		return month+" "+ time+" , "+year;
	}
	
	public static String getUIUpdatedTime(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = c.get(Calendar.MONTH)+1;
		String day   = getMonthDay(date);
		String year  = getYear(date);
		String time  = getTime(date);
		return month+"/"+day+"/"+year+" "+time;
	}
	
	public static String getUIWeekDateTime(Date date){
		String week = getWeekAllStr(date);
		String month = getMonthStr(date);
		String time = getMonthDay(date);
		String year = getYear(date);
		
		return week+", "+ time+"  "+ month+"  "+year;
	}
	
	public static String getUIWeekDateMonth(Date date){
		String week = getWeekStr(date);
		String month = getMonthStr(date);
		String time = getMonthDay(date);
		String year = getYear(date);
		
		return week+", "+"  "+ month+"  "+time;
	}
	
	public static String getUIWeekDateYearMonth(Date date){
		String week = getWeekALL(date);
		String month = getMonthStr(date);
		String time = getMonthDay(date);
		String year = getYear(date);
		String ampm = getUIAMPMTime(date);
		
		return week+", "+" "+ time+" "+month+" "+year+" "+ampm;
	}
	
	public static String getUINoDayDateYearMonth(Date date){
		String week = getWeekALL(date);
		String month = getMonthStr(date);
		String time = getMonthDay(date);
		String year = getYear(date);
		String ampm = getUIAMPMTime(date);
		
		return time+" "+month+" "+year+" "+ampm;
	}
	
	public static String getUIWeekDate(Date date){
		String week = getWeekStr(date);
		String month = getMonthStr(date);
		String time = getMonthDay(date);
 
		return week+", "+ month+" "+time;
	}
	
	
	public static int[] getScroCalendar(int year, int month) {
		boolean tip = true;
		int[] days = new int[2];
		int day = 0; // 澶�
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
			tip = true;
		} else {
			tip = false;

		}
		/**
		 * 杈撳叆骞翠唤璺濈1900骞�鏈�鏃ョ殑澶╂暟
		 */
		int sumyearday = 0; // 杈撳叆骞翠唤璺�900骞寸殑澶╂暟
		for (int i = 1900; i < year; i++) {
			if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
				sumyearday = sumyearday + 366;
			} else {
				sumyearday = sumyearday + 365;
			}
		}
		// System.out.print(" 璺濈1900骞�鏈�鏃� + sumyearday + "澶�);

		/**
		 * 杈撳叆鏈堜唤璺濇湰骞�鏈堢殑澶╂暟
		 */
		int summonth = 0;
		for (int i = 1; i <= month; i++) {
			switch (i) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				day = 31;
				break;
			case 2: // 骞冲勾2鏈�8澶�闂板勾2鏈�9澶�
				if (tip) {
					day = 29;
				} else {
					day = 28;
				}
				break;
			default:
				day = 30;
				break;
			}
			if (i < month) {
				summonth = summonth + day;
			}
		}
		int sumday = summonth + sumyearday; // Calculate the number of days this
		// int sumday = summonth +
		// sumyearday; // Calculate the
		// number of days this
		days[0] = day;
		days[1] = sumday;
		return days;
	}

	public static int getIntMonth(String month) {
		int monthint = 0;
		if ("January".equals(month)) {
			monthint = 1;
		} else if ("February".equals(month)) {
			monthint = 2;
		} else if ("March".equals(month)) {
			monthint = 3;
		} else if ("April".equals(month)) {
			monthint = 4;
		} else if ("May".equals(month)) {
			monthint = 5;
		} else if ("June".equals(month)) {
			monthint = 6;
		} else if ("July".equals(month)) {
			monthint = 7;
		} else if ("August".equals(month)) {
			monthint = 8;
		} else if ("September".equals(month)) {
			monthint =9;
		} else if ("October".equals(month)) {
			monthint = 10;
		} else if ("November".equals(month)) {
			monthint = 11;
		} else if ("December".equals(month)) {
			monthint = 12;
		}
		return monthint;
	}
	
	public static String getPrevCountdown(String timeZoneDate){
		String temp = "";
		Calendar cal = Calendar.getInstance();
		Date today = cal.getTime();
		long currentTime = today.getTime();
		Date date = DateUtil.getDate(timeZoneDate);
		long diff = currentTime-date.getTime();
		if(diff<0)
			temp = "Live...";
		else{
			if(DateFormat.toTheDay(diff)>0){
				temp += DateFormat.toTheDay(diff)+" Day ";
			    if(DateFormat.toTheHour(diff)>0)
				    temp += DateFormat.toTheHour(diff)+" Hours ";
			}else{
				if(DateFormat.toTheHour(diff)>0)
					temp += DateFormat.toTheHour(diff)+" Hours ";
				if(DateFormat.toTheMin(diff)>0)
					temp += DateFormat.toTheMin(diff)+" Mins ";
			}
			temp+="ago";
			
		}
		return temp;
	}
	
	public static String getNextCountdown(String timeZoneDate){
		String temp = "";
		Calendar cal = Calendar.getInstance();
		Date today = cal.getTime();
		long currentTime = today.getTime();
		Date date = DateUtil.getDate(timeZoneDate);
		long diff = date.getTime()-currentTime;
		if(diff<0)
			temp = "Game will begin shortly.";
		else
			temp = DateFormat.toTheDay(diff)+"D "+DateFormat.toTheHour(diff)+"H "+DateFormat.toTheMin(diff)+"M "+DateFormat.toTheSec(diff)+"S";
		return temp;
	}
	
	public static String getDMYT(String timeZoneDate){
		Date date = DateUtil.getDate(timeZoneDate);
		return getMonthDay(date)+" "+getMonth(date)+" "+getYear(date)+" | "+getWeekStr(date);
	}
	
	public static String getDMY(String timeZoneDate){
		Date date = DateUtil.getDate(timeZoneDate);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = c.get(Calendar.MONTH)+1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		return getYear(date)+"-"+getShowTime(month)+"-"+getShowTime(day);
	}
	
	public static String getWYD(String timeZoneDate){
		Date date = DateUtil.getDate(timeZoneDate);
		return getMonthDay(date) + "," + getMonth(date);
	}
	public boolean isLeapYear(int yearNum){
		boolean isLeap = false;
		if(yearNum % 4==0 && yearNum % 100 != 0){
			isLeap = true;
		}
		else if (yearNum %400==0){
			isLeap = true;
		}
		else {
			isLeap = false;
		}
		return isLeap;
	}
	
	public static String operDate(String date){
		String dateStr = date.trim();
		@SuppressWarnings("unused")
		int i = dateStr.indexOf("T");
		String str1 = dateStr.substring(0, 4);//get year
		String str2 = dateStr.substring(5, 7);//get month
		String str3 = dateStr.substring(8, 10);//get day
	    if(str2.equals("01"))
	    	str2 = "January";
	    if(str2.equals("02"))
	    	str2 = "February";
	    if(str2.equals("03"))
	    	str2 = "March";
	    if(str2.equals("04"))
	    	str2 = "April";
	    if(str2.equals("05"))
	    	str2 = "May";
	    if(str2.equals("06"))
	    	str2 = "June";
	    if(str2.equals("07"))
	    	str2 = "July";
	    if(str2.equals("08"))
	    	str2 = "August";
	    if(str2.equals("09"))
	    	str2 = "September";
	    if(str2.equals("10"))
	    	str2 = "October";
	    if(str2.equals("11"))
	    	str2 = "November";
	    if(str2.equals("12"))
	    	str2 = "December";
		return str2+" "+str3+","+str1;
	}
	
	public static int dip2px(Context context,float dipValue){
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int)(dipValue*scale+0.5f);
	}
	
}
